/* Copyright 2007-2008 dnAnalytics Project.
 *
 * Contributors to this file:
 * Patrick van der Velde
 * Marcus Cuda - http://marcus.cuda.net
 *
 * This file is part of dnAnalytics.  dnAnalytics is licensed under the 
 * Microsoft Public License. See License.txt for a complete copy of the
 * license.
 */
namespace dnAnalytics.LinearAlgebra.Decomposition
{
    internal class DenseLU : AbstractLU
    {
        public DenseLU(Matrix matrix) : base(matrix)
        {
        }

        protected override void DoCompute(Matrix data, int[] pivots)
        {
            DenseMatrix matrix = (DenseMatrix) data;
            SafeNativeMethods.d_lu_factor(matrix.Rows, matrix.Data, pivots);
        }

        private static void Solve(int order, int columns, double[] factor, int[] pivots, double[] rightSide)
        {
            SafeNativeMethods.d_lu_solve(order, columns, factor, pivots, rightSide);
        }

        protected override Matrix ComputeInverse(Matrix data, int[] pivots)
        {
            DenseMatrix result = new DenseMatrix(data);
            SafeNativeMethods.d_lu_inverse(data.Rows, result.Data, pivots);
            return result;
        }

        protected override void Solve(Matrix factor, int[] pivots, Matrix result)
        {
            Solve(factor.Rows, result.Columns, ((DenseMatrix) factor).Data, pivots, ((DenseMatrix) result).Data);
        }

        protected override void Solve(Matrix factor, int[] pivots, Vector result)
        {
            Solve(factor.Rows, 1, ((DenseMatrix) factor).Data, pivots, ((DenseVector) result).Data);
        }
    }
}